home *** CD-ROM | disk | FTP | other *** search
- Path: engnews1.Eng.Sun.COM!taumet!clamage
- From: clamage@Eng.sun.com (Steve Clamage)
- Newsgroups: comp.std.c++
- Subject: Re: Enumerated type converted to pointer? Lega
- Date: 8 Mar 1996 21:20:40 GMT
- Organization: Sun Microsystems Inc.
- Approved: clamage@eng.sun.com (comp.std.c++)
- Message-ID: <4hpuhm$ets@engnews1.Eng.Sun.COM>
- References: <4hobji$mco@netlab.cs.rpi.edu>
- Reply-To: clamage@Eng.sun.com
- NNTP-Posting-Host: taumet.eng.sun.com
- Content-Type: text
- X-Nntp-Posting-Host: taumet.eng.sun.com
- Content-Length: 1599
- X-Lines: 51
- Originator: clamage@taumet
-
- In article mco@netlab.cs.rpi.edu, bstowers@pobox.com (Brad Stowers) writes:
- >#include <stdio.h>
- >
- >typedef enum { mdType1, mdType2, mdType3 } myType;
- >
- >void FooBar(int x,
- > int* y,
- > myType type = mdType1)
- >{
- > printf("x=%i\ny=%p\ntype=%type\n", x, y, type);
- >}
- >
- >main()
- >{
- > int x = 10;
- > int* y = &x;
- > FooBar(x, y, mdType2);
- > FooBar(x, mdType1);
- >}
-
- > Is it just me, or should the last line
- >of the main() function ( FooBar(x, mdType1); ) have caused a type mismatch
- >error?
-
- The code is valid (although incorrect) as it stands.
-
- > I think I know why it didn't: mdType1 evaluates to an integer with
- >a value of 0, which is a valid value to pass as a pointer.
-
- That's about right. In more detail: Parameter y needs an int*, but the
- actual parameter is a constant of enumerated type. In an expression, an
- enumerator gets promoted to type int (or to a wider type if necessary),
- so at this point we have an int constant with value zero. The definition
- of "null pointer constant" is an integer constant-expression with value zero,
- so mdType1 qualifies for conversion to a null int*.
-
- >Obviously, the work-around is simple:
- >
- > typedef enum { mdType1 = 1, mdType2, mdType3 } myType;
- >
- >While this will solve the problem, I really think that it is the compiler's
- >job to see that mdType1 does not evaluate to the proper type no matter what
- >it's internal representation might be.
-
- It isn't the internal representation, but the zero value. This is
- yet another C compatibility hack that tends to subvert the type system.
-
- ---
- Steve Clamage, stephen.clamage@eng.sun.com
-
-
-
-
- [ comp.std.c++ is moderated. To submit articles: Try just posting with your
- newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
- comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
- Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu
- ]
-